package com.blog4j.article.api;

import cn.dev33.satoken.annotation.SaIgnore;
import com.blog4j.article.entity.ArticleEntity;
import com.blog4j.article.service.ArticleService;
import com.blog4j.article.vo.req.ArticleByCategoryIdReqVo;
import com.blog4j.article.vo.req.FavoriteArticleByUserIdReqVo;
import com.blog4j.article.vo.req.OrganizationArticleListReqVo;
import com.blog4j.article.vo.req.PortalArticleListReqVo;
import com.blog4j.article.vo.req.UserArticleReqVo;
import com.blog4j.article.vo.resp.ArticleCategoryNumsRespVo;
import com.blog4j.article.vo.resp.ArticleDetailRespVo;
import com.blog4j.article.vo.resp.ArticleRespVo;
import com.blog4j.common.annotation.OperationLog;
import com.blog4j.common.enums.ModuleTypeEnum;
import com.blog4j.common.model.Result;
import com.blog4j.requestRate.annotation.RequestRate;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @author 98k灬
 * @version v1.0.0
 * @Description : 功能描述
 * @Create on : 2024/7/21 21:31
 **/
@RestController
@RequestMapping("/api")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class ApiArticleController {
    private final ArticleService articleService;

    /**
     * 获取文章列表(门户端)
     *
     * @param reqVo 查询条件
     * @return 文章列表
     */
    @RequestRate
    @OperationLog(module= ModuleTypeEnum.ARTICLE, description = "获取文章列表(门户端)")
    @SaIgnore
    @PostMapping("/portalArticleList")
    public Result portalArticleList(@RequestBody PortalArticleListReqVo reqVo) {
        PageInfo<ArticleRespVo> pageInfo = articleService.getPortalArticleList(reqVo);
        return Result.ok(pageInfo);
    }

    /**
     * 根据文章ID获取文章信息
     *
     * @param articleId 文章ID
     * @return 文章信息
     */
    @RequestRate
    @OperationLog(module= ModuleTypeEnum.ARTICLE, description = "根据文章ID获取文章信息")
    @GetMapping("/info/{articleId}")
    public Result info(@PathVariable("articleId") String articleId) {
        ArticleDetailRespVo respVo = articleService.articleInfo(articleId);
        return Result.ok(respVo);
    }

    /**
     * 获取排行榜的文章
     *
     * @return 文章列表
     */
    @RequestRate
    @OperationLog(module= ModuleTypeEnum.ARTICLE, description = "获取排行榜的文章")
    @SaIgnore
    @GetMapping("/getViewsArticle")
    public Result getWeekViewsArticle() {
        List<ArticleEntity> list = articleService.getViewsArticle();
        return Result.ok(list);
    }

    /**
     * 文章浏览量
     *
     * @param articleId 文章ID
     * @return 成功
     */
    @RequestRate
    @OperationLog(module= ModuleTypeEnum.ARTICLE, description = "获取文章浏览量")
    @SaIgnore
    @GetMapping("/article_views/{articleId}")
    public Result articleViews(@PathVariable("articleId") String articleId) {
        articleService.articleViews(articleId);
        return Result.ok();
    }

    /**
     * 获取当前文章的上一篇和下一篇
     *
     * @param articleId 文章ID
     * @return 文章信息
     */
    @RequestRate
    @OperationLog(module= ModuleTypeEnum.ARTICLE, description = "获取当前文章的上一篇和下一篇")
    @SaIgnore
    @GetMapping("/getNextAndPreviousArticle/{articleId}")
    public Result getNextAndPreviousArticle(@PathVariable("articleId") String articleId) {
        Map<String, ArticleRespVo> resMap = articleService.getNextAndPreviousArticle(articleId);
        return Result.ok(resMap);
    }

    /**
     * 获取推荐文章列表
     *
     * @param articleId 文章ID
     * @return 推荐文章列表
     */
    @RequestRate
    @OperationLog(module= ModuleTypeEnum.ARTICLE, description = "获取推荐文章列表")
    @SaIgnore
    @GetMapping("/getRecommendArticle/{articleId}")
    public Result getRecommendArticle(@PathVariable("articleId") String articleId) {
        List<ArticleRespVo> list = articleService.getRecommendArticle(articleId);
        return Result.ok(list);
    }

    /**
     * 获取最新文章列表
     *
     * @return 最新文章列表
     */
    @RequestRate
    @OperationLog(module= ModuleTypeEnum.ARTICLE, description = "获取最新文章列表")
    @SaIgnore
    @GetMapping("/getNewArticleList")
    public Result getNewArticleList() {
        List<ArticleRespVo> list = articleService.getNewArticleList();
        return Result.ok(list);
    }

    /**
     * 根据分类ID获取文章信息
     *
     * @param reqVo 请求信息
     * @return 文章信息
     */
    @RequestRate
    @OperationLog(module= ModuleTypeEnum.ARTICLE, description = "根据分类ID获取文章信息")
    @SaIgnore
    @PostMapping("/getByCategoryId")
    public Result getByCategoryId(@RequestBody @Valid ArticleByCategoryIdReqVo reqVo) {
        PageInfo<ArticleRespVo> pageInfo = articleService.getByCategoryId(reqVo);
        return Result.ok(pageInfo);
    }

    /**
     * 根据组织ID获取组织文章列表
     *
     * @param reqVo 请求
     * @return 组织信息
     */
    @RequestRate
    @OperationLog(module= ModuleTypeEnum.ARTICLE, description = "根据组织ID获取组织文章列表")
    @SaIgnore
    @PostMapping("/organizationArticleList")
    public Result organizationArticleList(@RequestBody @Valid OrganizationArticleListReqVo reqVo) {
        PageInfo<ArticleRespVo> pageInfo = articleService.organizationArticleList(reqVo);
        return Result.ok(pageInfo);
    }

    /**
     * 获取用户的文章
     *
     * @param reqVo 请求信息
     * @return 用户文章
     */
    @RequestRate
    @OperationLog(module= ModuleTypeEnum.ARTICLE, description = "获取用户的文章")
    @PostMapping("/getUserArticle")
    public Result getUserArticle(@RequestBody @Valid UserArticleReqVo reqVo) {
        PageInfo<ArticleRespVo> pageInfo = articleService.getUserArticle(reqVo);
        return Result.ok(pageInfo);
    }

    /**
     * 获取用户的文章数、分类数
     *
     * @return 用户的文章数、分类数
     */
    @RequestRate
    @OperationLog(module= ModuleTypeEnum.ARTICLE, description = "获取用户的文章数、分类数")
    @GetMapping("/getArticleCategoryNums")
    public Result getArticleCategoryNums(@RequestParam(value = "userId") String userId) {
        ArticleCategoryNumsRespVo respVo = articleService.getArticleCategoryNums(userId);
        return Result.ok(respVo);
    }

    /**
     * 查询用户的收藏文章
     *
     * @param reqVo 请求信息
     * @return 收藏文章
     */
    @RequestRate
    @OperationLog(module= ModuleTypeEnum.ARTICLE, description = "获取用户的收藏文章")
    @PostMapping("/getFavoriteArticleByUserId")
    public Result getFavoriteArticleByUserId(@RequestBody @Valid FavoriteArticleByUserIdReqVo reqVo) {
        PageInfo<ArticleRespVo> pageInfo = articleService.getFavoriteArticleByUserId(reqVo);
        return Result.ok(pageInfo);
    }
}
